package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Restaurant;
import com.ruoyi.system.domain.RestaurantOrder;
import com.ruoyi.system.domain.RestaurantOrderItem;
import com.ruoyi.system.domain.Tourist;
import com.ruoyi.system.enums.RestaurantOrderStatusEnum;
import com.ruoyi.system.mapper.RestaurantOrderMapper;
import com.ruoyi.system.service.IRestaurantOrderItemService;
import com.ruoyi.system.service.IRestaurantOrderService;
import com.ruoyi.system.service.IRestaurantService;
import com.ruoyi.system.service.ITouristService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 餐饮订单Service业务层处理
 *
 * @author 强盛酒店
 * @date 2023-03-12
 */
@Service
public class RestaurantOrderServiceImpl implements IRestaurantOrderService {
    @Autowired
    private RestaurantOrderMapper restaurantOrderMapper;
    @Autowired
    private ITouristService touristService;
    @Autowired
    private IRestaurantOrderItemService itemService;
    @Autowired
    private IRestaurantService restaurantService;

    /**
     * 查询餐饮订单
     *
     * @param id 餐饮订单主键
     * @return 餐饮订单
     */
    @Override
    public RestaurantOrder selectRestaurantOrderById(Long id) {
        return restaurantOrderMapper.selectRestaurantOrderById(id);
    }

    /**
     * 查询餐饮订单列表
     *
     * @param restaurantOrder 餐饮订单
     * @return 餐饮订单
     */
    @Override
    public List<RestaurantOrder> selectRestaurantOrderList(RestaurantOrder restaurantOrder) {
        List<RestaurantOrder> restaurantOrders = restaurantOrderMapper.selectRestaurantOrderList(restaurantOrder);
        for (RestaurantOrder order : restaurantOrders) {
            String res = "";
            List<RestaurantOrderItem> restaurantOrderItems = itemService.selectRestaurantOrderItemList(new RestaurantOrderItem() {{
                setOrderId(order.getId());
            }});
            for (RestaurantOrderItem restaurantOrderItem : restaurantOrderItems) {
                Restaurant restaurant = restaurantService.selectRestaurantById(restaurantOrderItem.getRestaurantId());
                res =res + restaurant.getName()+"("+restaurantOrderItem.getRestaurantNum()+")份\n";
            }
            order.setRes(res);
        }
        return restaurantOrders;
    }

    /**
     * 新增餐饮订单
     *
     * @param restaurantOrder 餐饮订单
     * @return 结果
     */
    @Override
    public int insertRestaurantOrder(RestaurantOrder restaurantOrder) {
        restaurantOrder.setCreateTime(DateUtils.getNowDate());
        return restaurantOrderMapper.insertRestaurantOrder(restaurantOrder);
    }

    /**
     * 修改餐饮订单
     *
     * @param restaurantOrder 餐饮订单
     * @return 结果
     */
    @Override
    public int updateRestaurantOrder(RestaurantOrder restaurantOrder) {
        restaurantOrder.setUpdateTime(DateUtils.getNowDate());
        return restaurantOrderMapper.updateRestaurantOrder(restaurantOrder);
    }

    /**
     * 批量删除餐饮订单
     *
     * @param ids 需要删除的餐饮订单主键
     * @return 结果
     */
    @Override
    public int deleteRestaurantOrderByIds(Long[] ids) {
        return restaurantOrderMapper.deleteRestaurantOrderByIds(ids);
    }

    /**
     * 删除餐饮订单信息
     *
     * @param id 餐饮订单主键
     * @return 结果
     */
    @Override
    public int deleteRestaurantOrderById(Long id) {
        return restaurantOrderMapper.deleteRestaurantOrderById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(List<Restaurant> restaurantList) {
        String remark = SecurityUtils.getLoginUser().getUser().getRemark();
        Tourist tourist = touristService.selectTouristById(Long.parseLong(remark));
        RestaurantOrder restaurantOrder = new RestaurantOrder();
        restaurantOrder.setUserName(tourist.getName());
        restaurantOrder.setPhone(tourist.getPhone());
        restaurantOrder.setIdCard(tourist.getIdCard());
        restaurantOrder.setStatus(RestaurantOrderStatusEnum.ON.getCode());
        restaurantOrder.setCreateTime(new Date());
        restaurantOrder.setCreateUser(SecurityUtils.getUserId());
        double price = 0;
        for (Restaurant restaurant : restaurantList) {
            double i = restaurant.getNum() * Double.parseDouble(restaurant.getPrice());
            price = price + i;
        }
        restaurantOrder.setOrderPrice(String.valueOf(price));
        int i = restaurantOrderMapper.insertRestaurantOrder(restaurantOrder);
        for (Restaurant restaurant : restaurantList) {
            RestaurantOrderItem item = new RestaurantOrderItem();
            item.setOrderId(restaurantOrder.getId());
            item.setRestaurantId(restaurant.getId());
            item.setPrice(restaurant.getPrice());
            item.setRestaurantNum(restaurant.getNum());
            itemService.insertRestaurantOrderItem(item);
            Restaurant old = restaurantService.selectRestaurantById(restaurant.getId());
            old.setStockNum(old.getStockNum() - restaurant.getNum());
            restaurantService.updateRestaurant(old);
        }
        return i;
    }

    @Override
    public int auditRestaurantOrder(Long id) {
        RestaurantOrder restaurantOrder = restaurantOrderMapper.selectRestaurantOrderById(id);
        restaurantOrder.setStatus(RestaurantOrderStatusEnum.OFF.getCode());
        return restaurantOrderMapper.updateRestaurantOrder(restaurantOrder);
    }

    @Override
    public List<RestaurantOrder> getData(String day) {
        return restaurantOrderMapper.getData(day);
    }

    @Override
    public List<RestaurantOrder> getMyOrder() {
        Long userId = SecurityUtils.getUserId();
        List<RestaurantOrder> restaurantOrderList = restaurantOrderMapper.selectRestaurantOrderList(new RestaurantOrder() {{
            setCreateUser(userId);
        }});
        for (RestaurantOrder restaurantOrder : restaurantOrderList) {
            List<RestaurantOrderItem> restaurantOrderItems = itemService.selectRestaurantOrderItemList(new RestaurantOrderItem() {{
                setOrderId(restaurantOrder.getId());
            }});
            String res = "";
            for (RestaurantOrderItem restaurantOrderItem : restaurantOrderItems) {
                Restaurant restaurant = restaurantService.selectRestaurantById(restaurantOrderItem.getRestaurantId());
                res =res + restaurant.getName()+"("+restaurantOrderItem.getRestaurantNum()+")份\n";
            }
            restaurantOrder.setRes(res);
            for (RestaurantOrderItem restaurantOrderItem : restaurantOrderItems) {
                Restaurant restaurant = restaurantService.selectRestaurantById(restaurantOrderItem.getRestaurantId());
                restaurantOrderItem.setRestaurant(restaurant);
            }
            restaurantOrder.setRestaurantOrderItems(restaurantOrderItems);
        }
        return restaurantOrderList;
    }

    @Override
    public int cancel(Long id) {
        RestaurantOrder restaurantOrder = restaurantOrderMapper.selectRestaurantOrderById(id);
        List<RestaurantOrderItem> restaurantOrderItemList = itemService.selectRestaurantOrderItemList(new RestaurantOrderItem() {{
            setOrderId(restaurantOrder.getId());
        }});
        for (RestaurantOrderItem restaurantOrderItem : restaurantOrderItemList) {
            Restaurant restaurant = restaurantService.selectRestaurantById(restaurantOrderItem.getRestaurantId());
            restaurant.setStockNum(restaurant.getStockNum()+restaurantOrderItem.getRestaurantNum());
            restaurantService.updateRestaurant(restaurant);
        }
        restaurantOrder.setStatus(RestaurantOrderStatusEnum.CANCEL.getCode());
        return restaurantOrderMapper.updateRestaurantOrder(restaurantOrder);
    }
}
